/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wiiscript;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 *
 * @author Evan
 */
public class MacSheet {
    public static int CONFIRM_SAVE = 1;
    public static int DONT_SAVE = JOptionPane.NO_OPTION;
    public static int SAVE = JOptionPane.YES_OPTION;
    public static int CANCEL = JOptionPane.CANCEL_OPTION;

    private static int returnValue = -1;

    private JDialog sheet;
    private JLabel messageText = new JLabel();
    private JLabel subMessageText = new JLabel();
    private JLabel iconLabel = new JLabel();
    JPanel buttonPanel = new JPanel();
    JPanel messagePanel = new JPanel();
    public MacSheet(Window parentWindow) {

        sheet = new JDialog(parentWindow, Dialog.ModalityType.DOCUMENT_MODAL);
        sheet.getRootPane().putClientProperty("apple.awt.documentModalSheet", true);
        sheet.setPreferredSize(new Dimension(450, 150));
        sheet.setResizable(false);

        messageText.setUI(new MultiLineLabelUI());
        messageText.setBorder(BorderFactory.createEmptyBorder(10, 10, 8, 10)); //top, left, bottom, right
        messageText.setFont(new Font("Lucida Grande", Font.BOLD, 13));
        messageText.setAlignmentX(Component.TOP_ALIGNMENT);

        subMessageText.setUI(new MultiLineLabelUI());
        subMessageText.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
        subMessageText.setFont(new Font("Lucida Grande", Font.PLAIN, 11));
        subMessageText.setAlignmentY(Component.LEFT_ALIGNMENT);

        messagePanel.add(messageText);
        messagePanel.add(subMessageText);
        messagePanel.setAlignmentY(Component.LEFT_ALIGNMENT);
        messagePanel.setAlignmentX(Component.TOP_ALIGNMENT);

        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 87, 0, 0));

        iconLabel.setBorder(BorderFactory.createEmptyBorder(10, 25, 5, 5));
        sheet.getContentPane().add(iconLabel ,BorderLayout.WEST);
        sheet.getContentPane().add(messagePanel, BorderLayout.CENTER);
        sheet.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }
    public void setMainMessage(String message) {
        messageText.setText(message);
    }
    public void setSubMessage(String message) {
        subMessageText.setText(message);
    }
    public void setIcon(Icon icon) {
        iconLabel.setIcon(icon);
    }
    public void setVisible(boolean flag) {
        sheet.pack();
        sheet.setVisible(flag);
    }
    public void setPerferredSize(Dimension size) {
        sheet.setPreferredSize(size);
    }
    public void setButtons(JComponent[] buttons, int[] values) {
        buttonPanel.removeAll();
        for(int i  = 0; i < buttons.length; i++) {
            if(buttons[i] instanceof JButton) {
                ((JButton)buttons[i]).addActionListener(new ButtonListener(values[i]));
            } else {
                JButton button = findButton(buttons[i]);
                button.addActionListener(new ButtonListener(values[i]));
            }
            buttonPanel.add(buttons[i]);
        }
    }
    public void setSelected(int index) {
        ((JButton)buttonPanel.getComponent(index)).setSelected(true);
    }
    private JButton findButton(JComponent component) {
        if(component instanceof JButton) {
            return (JButton)component;
        } else if(component.getComponentCount() > 0) {
            Component[] components = component.getComponents();
            for(Component c : components) {
                JButton b = findButton((JComponent)c);
                if(b != null) return b;
            }
        }
        return null;
    }
    public static int showOptionDialog(Window parentWindow, String mainMessage,
            String subMessage, int optionType, int messageType) {
        MacSheet mSheet = new MacSheet(parentWindow);

        switch(messageType) {
            case JOptionPane.QUESTION_MESSAGE:
                mSheet.setIcon(UIManager.getIcon("OptionPane.questionIcon"));
                break;
            case JOptionPane.WARNING_MESSAGE:
                mSheet.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
                break;
            case JOptionPane.ERROR_MESSAGE:
                mSheet.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
                break;
            default:        
        }     
        mSheet.setMainMessage(mainMessage);
        mSheet.setSubMessage(subMessage);
        
        if(optionType == CONFIRM_SAVE) {
            //Cancel button needs extra padding on the left
            JButton cancelButton = new JButton("Cancel");
            JPanel padding = new JPanel();
            padding.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
            padding.add(cancelButton);

            

            JComponent[] buttons = {new JButton("Don't Save"), padding,
            new JButton("Save...")};
            int[] values = {DONT_SAVE, CANCEL, SAVE};
            mSheet.setButtons(buttons, values);

            //Save button needs to be selected
            mSheet.setSelected(2);
        }
        mSheet.setVisible(true);
        return returnValue;
    }
    private class ButtonListener implements ActionListener {
        private int button;
        public ButtonListener(int button) {
            this.button = button;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            returnValue = button;
            sheet.dispose();
        }
    }
}
